<?php
    class Search extends CI_Controller{
    	function index(){
    		doSearch();
    	}
		function doSearch(){
			$stringToSearch = $this->input->post('searchString');
			$searchType = $this->input->post('search');
			
			if($searchType == 'idea'){
				redirect(base_url("search/idea/$stringToSearch"));
			}else{
				redirect(base_url("search/user/$stringToSearch"));
			}
		}
		function idea($ideaString = NULL){
			//check if string is null
			if($ideaString === NULL){
				//do nothing
			}else{
				$ideaString = str_replace("%20", " ", $ideaString);
				
				//do search
				$sortBy = $this->input->post('sortBy');
				$orderMethod = $this->input->post('sortMethod');
				$category = $this->input->post('category');
				if(strlen($category) < 1){
					$category = "uncategorised";
				}
				if($sortBy == NULL){
					$sortBy = 'Relevance';
				}
				$this->load->model('idea_model');
				$data['ideas'] = $this->idea_model->search($ideaString, $sortBy, $orderMethod, $category);
				$data['searchString'] = $ideaString;
				$data['sort'] = $sortBy;
				$this->load->view('view_result', $data);
			}
		}
		
		function user($userString = NULL)
		{
			if($userString === NULL)
			{
				//do nothing
			}
			else {
				$this->load->model('user_model');
				$data['users']= (array)$this->user_model->search($userString);
				$data['userString'] = $userString;
				$this->load->view('view_resultuser', $data);
			}
			
		}
		
		
    }
?>